<?php
/**
 * @authotr 刘杰
 * @since 1.0
 */
namespace Admin\Controller;

use Admin\Model\Admin;
use Common\Model\Movie;
use Think\Controller;
use Think\Model;

/**
 * 电影管理
 * @authotr 刘杰
 * @since 1.0
 */
class MovieController extends BaseController
{
    public function index()
    {
        //搜索+分页
        if(!empty($_GET['name'])){
            $condition['name'] = $_GET['name'];
        }
        //调用 定义好的 状态常量属性和方法 状态为10
        $count = M('Movie')->where($condition)->count();
        //实例化一个分页类
        $page = new \Think\Page($count, 10);
        $list = M('movie')->limit($page->firstRow . ',' . $page->listRows)->where($condition)->select();
        //分页
        $show = $page->show();
        $this->assign('show', $show);

        //获取 定义的常量里面定义的方法  状态  // 标签
        foreach ($list as $key => $val) {
            $list[$key]['status'] = Movie::statusAlias($val['status']);//状态
            $list[$key]['tags']  = Movie::tagsAlias($val['tags']);    // 标签
            $list[$key]['language']  = Movie::languageAlias($val['language']);    // 语言
            $list[$key]['cover'] =$this->substr($val['cover']).'96_'.$val['cover']; //图片
        }
        $this->assign('list', $list);

        //发送 status 状态  调用定义好的 类里面的常量
        //用于在首页显示用户状态 或在模版里直接输出<?php echo \Admin\Model\Admin::user()['username'];?\>
        $user = Admin::user()['username'];
        $this->assign('user', $user);
        $this->display();
    }

    /**
     * 添加页面
     */
    public function create()
    {
        //状态
        $status = Movie::statusAlias();
        $this->assign('status', $status);
        //标签
        $tags = Movie::tagsAlias();
        $this->assign('tags', $tags);
        //语言
        $language = Movie::languageAlias();
        $this->assign('language', $language);
        $this->display();
    }

    /*
     * 提交数据
     */
    public function add()
    {
        foreach ($_POST as $key => $val) {
            if (empty($val)) {
                $this->assign('message', '表单不能为空');
                $this->display('Movie/create');
            }
        }
        $data['name'] = $_POST['name'];                // 电影名称
        $data['actor'] = $_POST['actor'];             // 演员列表
        $data['long'] = $_POST['long'];                // 电影时长（分钟
        $data['release_at'] = $_POST['release_at'];    // 上映时间
        $data['cover'] = $this->upload();
        $data['language'] = $_POST['language'];         // 语言 拼接
        // 标签 拼接
        $labe='';
        foreach ($_POST['tags'] as $key => $val) {
            $labe .= $val . ',';
        }
        $labe = trim($labe, ',');
        $data['tags'] = $labe;
        $data['status'] = $_POST['status'];           // 状态
        $data['summary'] = $_POST['summary'];         // 简介
        $data['description'] = $_POST['description']; // 描述
        $data['created_at'] = time(); // 新增时间
        $movie = D('Movie');
        $list = $movie->getAll($data);
        if($list){
            $this->redirect('Movie/index');
        }
    }
    /**
     * 修改页面
     */

    public function update()
    {
        //把该电影的数据 加载到 修改模版
        $where['id'] = $_GET['id'];
        $this->assign('id',$where['id']);
        $find = D('Movie');
        $finds = $find->findMovie($where);
        $this->assign('finds',$finds);

        //状态
        $status = Movie::statusAlias();
        $this->assign('status', $status);
        //标签
        $tags = Movie::tagsAlias();
        $this->assign('tags', $tags);
        //语言
        $language = Movie::languageAlias();
        $this->assign('language', $language);
        $this->display();
    }
    /**
     * 修改
     * @return array
     */

    public function up(){
        $where['id'] = $_GET['id'];

        $find = D('Movie');
        // 处理标签
        if( array($_POST['tags'])){
         foreach($_POST['tags'] as $ke =>$val){
             $dat .=$val.',';
            }
        }
        $dat = trim($dat,',');
        $data =$_POST;
        $data['tags'] =  $dat;
        $data['created_at'] =  time();
        $data['cover'] =  $this->upload();
        var_dump($data);
        $finds = $find->setAll($data,$where);
       if($finds){
            $this->redirect('Movie/index');
       }
    }
}